package secondFragment.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import java.security.PolicySpi;
import java.util.ArrayList;

import common.BaseActivity;
import common.Constant;
import personalPage.activity.PersonalPage;
import question.activity.QuestionDetail;
import secondFragment.bean.PopularQuestionBean;
import utils.MyLogUtils;
import view.MyImageLoader;
import volunteer.activity.Volunteer;

/**
 * Created by Administrator on 2017/2/21.
 * 热门提问的列表
 */

public class PqLvAdapter extends BaseAdapter {
    private ArrayList<PopularQuestionBean.ConsultListBean> arrayList;
    private Context context;
    private onPayClick onPayClick;
    private PraiseListener praiseListener;
    private OnQuestionDetailListener onQuestionDetailListener;
    private static final String TAG = "PqLvAdapter";

    public PqLvAdapter(Context context, ArrayList<PopularQuestionBean.ConsultListBean> arrayList, onPayClick onPayClick ,PraiseListener praiseListener,OnQuestionDetailListener onQuestionDetailListener) {
        this.context = context;
        this.arrayList = arrayList;
        this.onPayClick = onPayClick;
        this.praiseListener = praiseListener;
        this.onQuestionDetailListener = onQuestionDetailListener;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return super.getDropDownView(position, convertView, parent);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyHolder myHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.pq_lv_item, parent, false);
            myHolder = new MyHolder();
            myHolder.pq_lv_item_user_name = (TextView) convertView.findViewById(R.id.pq_lv_item_user_name);
            myHolder.pq_lv_item_user_title = (TextView) convertView.findViewById(R.id.pq_lv_item_user_title);
            myHolder.pq_lv_item_checked_num = (TextView) convertView.findViewById(R.id.pq_lv_item_checked_num);
            myHolder.pq_lv_item_volun_name = (TextView) convertView.findViewById(R.id.pq_lv_item_volun_name);
            myHolder.pq_lv_item_volun_title = (TextView) convertView.findViewById(R.id.pq_lv_item_volun_title);
            myHolder.pq_lv_item_content = (TextView) convertView.findViewById(R.id.pq_lv_item_content);
            myHolder.pq_lv_item_check_answer = (TextView) convertView.findViewById(R.id.pq_lv_item_check_answer);
            myHolder.pq_lv_item_user_icon = (ImageView) convertView.findViewById(R.id.pq_lv_item_user_icon);
            myHolder.pq_lv_item_volun_vip_ima = (ImageView) convertView.findViewById(R.id.pq_lv_item_volun_vip_ima);
            myHolder.pq_lv_item_volun_icon = (ImageView) convertView.findViewById(R.id.pq_lv_item_volun_icon);
            myHolder.pq_lv_item_zan = (TextView) convertView.findViewById(R.id.pq_lv_item_zan);
            myHolder.pq_lv_item_comment = (TextView) convertView.findViewById(R.id.pq_lv_item_comment);
            myHolder.pq_lv_item_pay = (TextView) convertView.findViewById(R.id.pq_lv_item_pay);
            myHolder.pq_lv_item_volun_answer = (TextView) convertView.findViewById(R.id.pq_lv_item_volun_answer);
            convertView.setTag(myHolder);
        } else {
            myHolder = (MyHolder) convertView.getTag();
        }

        String volun_title;
        if(TextUtils.isEmpty(arrayList.get(position).getDialogtype())){
            //是否显示vip图标
            myHolder.pq_lv_item_volun_vip_ima.setVisibility(View.GONE);
        }else{
            myHolder.pq_lv_item_volun_vip_ima.setVisibility(View.VISIBLE);
        }
        if (arrayList.get(position).getIs_prised() == 0) {
            //未点赞
            Drawable zan = context.getResources().getDrawable(R.mipmap.college_topic_item_zan_ima);
            zan.setBounds(0, 0, zan.getMinimumWidth(), zan.getMinimumHeight());
            myHolder.pq_lv_item_zan.setCompoundDrawables(zan,null,null,null);
        }else{
            Drawable zan = context.getResources().getDrawable(R.mipmap.college_topic_item_zan_ima1);
            zan.setBounds(0, 0, zan.getMinimumWidth(), zan.getMinimumHeight());
            myHolder.pq_lv_item_zan.setCompoundDrawables(zan,null,null,null);
        }
        myHolder.pq_lv_item_zan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                praiseListener.praiseListener(position);
            }
        });

        if ("free".equals(arrayList.get(position).getKind())) {
            //免费问题时
            myHolder.pq_lv_item_checked_num.setVisibility(View.GONE);
            volun_title = arrayList.get(position).getAnswercollege() + arrayList.get(position).getAnswermajor();
            myHolder.pq_lv_item_check_answer.setVisibility(View.GONE);
            myHolder.pq_lv_item_pay.setVisibility(View.GONE);
            myHolder.pq_lv_item_check_answer.setVisibility(View.GONE);
            myHolder.pq_lv_item_volun_answer.setVisibility(View.VISIBLE);
        } else {
            //付费问题时
            myHolder.pq_lv_item_pay.setText("¥ " + (Constant.QUESTIONWATCH_PRICE / 100.0));
            myHolder.pq_lv_item_checked_num.setVisibility(View.VISIBLE);
            myHolder.pq_lv_item_checked_num.setText("" + arrayList.get(position).getWatch());
            volun_title = arrayList.get(position).getAnswerbrief();
            if ("0".equals(arrayList.get(position).getIspay())) {
                //未付费
                myHolder.pq_lv_item_pay.setVisibility(View.VISIBLE);
                myHolder.pq_lv_item_check_answer.setVisibility(View.VISIBLE);
                myHolder.pq_lv_item_volun_answer.setVisibility(View.GONE);
            } else {
                //已经付费
                myHolder.pq_lv_item_pay.setVisibility(View.GONE);
                myHolder.pq_lv_item_check_answer.setVisibility(View.GONE);
                myHolder.pq_lv_item_volun_answer.setVisibility(View.VISIBLE);
            }
        }
        myHolder.pq_lv_item_volun_answer.setText(arrayList.get(position).getAnswer());
        myHolder.pq_lv_item_user_name.setText("" + arrayList.get(position).getAskername());
        String title = arrayList.get(position).getType() + "/" + arrayList.get(position).getCity();
        myHolder.pq_lv_item_user_title.setText(title);
        myHolder.pq_lv_item_content.setText(arrayList.get(position).getQuestion());
        MyImageLoader.showImage(arrayList.get(position).getAskerhead(), myHolder.pq_lv_item_user_icon);
        MyImageLoader.showImage(arrayList.get(position).getAnswerhead(), myHolder.pq_lv_item_volun_icon);
        myHolder.pq_lv_item_volun_name.setText("" + arrayList.get(position).getAnswername());
        myHolder.pq_lv_item_volun_title.setText("" + volun_title);
        myHolder.pq_lv_item_zan.setText(arrayList.get(position).getPraise());
        myHolder.pq_lv_item_comment.setText(arrayList.get(position).getComment());

        myHolder.pq_lv_item_user_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //提问者首页
                Intent i = new Intent(context, PersonalPage.class);
                i.putExtra("type", arrayList.get(position).getFrom());
                i.putExtra("userId", arrayList.get(position).getQuestioner_id());
                ((BaseActivity)context).startActivityForResult(i,0);
            }
        });
        myHolder.pq_lv_item_user_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //提问者首页
                Intent i = new Intent(context, PersonalPage.class);
                i.putExtra("type", arrayList.get(position).getFrom());
                i.putExtra("userId", arrayList.get(position).getQuestioner_id());
                context.startActivity(i);
            }
        });

        myHolder.pq_lv_item_volun_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //志愿者首页
            }
        });

        myHolder.pq_lv_item_check_answer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onPayClick.click(position);//外部处理
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到问题详情
                onQuestionDetailListener.onQuestionDetailListener(position);
            }
        });
        return convertView;
    }

    static class MyHolder {
        TextView pq_lv_item_volun_answer;//回答内容
        TextView pq_lv_item_pay;//钱
        ImageView pq_lv_item_user_icon;//头像
        ImageView pq_lv_item_volun_vip_ima;//vip
        TextView pq_lv_item_user_name;//提问者的名字
        TextView pq_lv_item_user_title;//提问者的位置
        TextView pq_lv_item_checked_num;//被查看多少次
        TextView pq_lv_item_content;//问题内容
        ImageView pq_lv_item_volun_icon;//回答者头像
        TextView pq_lv_item_check_answer;//付费看答案
        TextView pq_lv_item_volun_name;//志愿者名字
        TextView pq_lv_item_volun_title;//志愿者信息
        TextView pq_lv_item_zan;//赞
        TextView pq_lv_item_comment;//评论
    }

    public interface onPayClick {
        void click(int position);
    }

    public interface PraiseListener{
        void praiseListener(int position);
    }

    public interface OnQuestionDetailListener{
        void onQuestionDetailListener(int position);
    }
}
